package br.com.sgpv2.controler.secretaria;

import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import br.com.sgpv2.controler.SGPController;
import br.com.sgpv2.entity.JsonResponse;
import br.com.sgpv2.entity.organograma.Secretaria;
import br.com.sgpv2.service.entidade.EntidadeService;
import br.com.sgpv2.service.secretaria.SecretariaService;

/**
 * Handles requests for the application /secretarias.
 */

@Controller
public class SecretariaController extends SGPController {

	@Autowired
	private SecretariaService secretariaService;
	@Autowired
	private EntidadeService entidadeService;
	
	/**
	 * Página de secretarias - controller da página secretarias.
	 * @throws Exception.
	 */
	@RequestMapping(value = "/secretarias", method = RequestMethod.POST)
	public String secretariasPesquisar(@RequestParam String descricao, Locale locale, Model model) throws Exception {
		
		return listar(descricao, locale, model);
	}
	
	/**
	 * Página de secretarias - controller da página secretarias, acessada pelo menu.
	 * @throws Exception.
	 */
	@RequestMapping(value = "/secretarias", method = RequestMethod.GET)
	public String secretarias(Locale locale, Model model) throws Exception {
		
		return listar(null, locale, model);
	}
	
	private String listar(String descricao, Locale locale, Model model) {
		List<Secretaria> secretarias = secretariaService.consultarPorDescricao(descricao);

		configurarPaginacao(secretarias, model);
		model.addAttribute("secretarias", getPagedListHolder().getPageList());
		
		return "secretarias";
	}
	
	/**
	 * avança para página de inclusão de secretarias.
	 * @throws Exception 
	 */
	@RequestMapping(value = "/secretaria/salvar", method = RequestMethod.GET)
	public String salvar(Model model) throws Exception {
	
		model.addAttribute("secretaria", new Secretaria());
		
		return "secretaria_incluir";		
	}
	
	/**
	 * inclui uma secretaria no sistema.
	 * @throws Exception 
	 */
	@RequestMapping(value = "/secretaria/salvar", method = RequestMethod.POST)
	public String salvar(@ModelAttribute Secretaria secretaria, Model model, BindingResult result) throws Exception {
				
		if(secretaria != null){
			secretaria.setEntidade(entidadeService.buscarEntidade());	
			secretariaService.salvar(secretaria);
		}
		return "redirect:../secretarias";
	}
	
	/**
	 * avança para página de alteração de secretaria.
	 * @throws Exception 
	 */
	@RequestMapping(value = "/secretaria/alterar/{pk}", method = RequestMethod.GET)
	public String getAlterar(@PathVariable Integer pk, Model model) throws Exception {
		Secretaria secretariaAlteracao = secretariaService.buscar(pk);
		if(secretariaAlteracao != null){
			model.addAttribute("secretaria",secretariaAlteracao);			
			return "secretaria_alterar";
		}else{
			return "redirect:../secretaria";
		}		
	}
	
	/**
	 * avança para página de alteração de secretaria.
	 * @throws Exception 
	 */
	@RequestMapping(value = "/secretaria/alterar", method = RequestMethod.POST)
	public String alterar(@ModelAttribute Secretaria secretaria, Model model, BindingResult result) throws Exception {
			
		if(secretaria != null){
			secretaria.setEntidade(entidadeService.buscarEntidade());
			secretariaService.alterar(secretaria);
		}
		return "redirect:../secretarias";
	}
	
	/**
	 * Exclui uma Secretaria.
	 * @throws Exception 
	 */
	@RequestMapping(value = "/secretaria/excluir/{pk}", method = RequestMethod.GET)
	public String excluir(@PathVariable Integer pk, Model model) throws Exception {
		Secretaria secretariaExclusao = secretariaService.buscar(pk);
		
		if(secretariaExclusao !=null){
			secretariaService.excluir(secretariaExclusao);
		}
		
		return "redirect:/secretarias";
	}
	
	/**
	 * Valida a exclusao
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/secretaria/validarExclusao/{idSecretaria}", method = RequestMethod.GET)
	public JsonResponse validarExclusao(@PathVariable String idSecretaria, Model model) throws Exception {		
		JsonResponse jsonResponse = new JsonResponse();
		jsonResponse.setSuccess(secretariaService.validarExclusao(idSecretaria));				
		return jsonResponse;
	}
	
	@ResponseBody
	@RequestMapping(value = "/secretaria/paginacao/{acao}", method = RequestMethod.GET)
	public ModelAndView paginacao(@PathVariable String acao, Model model) throws Exception {		
		navegarPaginacao(acao, model);
		model.addAttribute("secretarias", getPagedListHolder().getPageList());
		return new ModelAndView("secretaria.lista");
	}
	
	
}
